package com.ryan.dispatcher.ability;

import com.ryan.dispatcher.Args;
import com.ryan.dispatcher.ResourceTable;
import com.ryan.dispatcher.comp.PagerFraction;
import com.ryan.ohos.extension.FractionManageHelper;
import com.ryan.ohos.extension.widget.viewpager.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.List;

public class ArgsAbility extends FractionAbility {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.TRANSPARENT.getValue());
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);

        String key = (String) getIntent().getParams().getParam(Args.ARG_KEY);
        int layout = getLayoutRes(key);
        if (layout <= 0) {
            terminateAbility();
            return;
        }
        setUIContent(layout);

        onInit(key);
    }

    @Override
    protected void onStop() {
        super.onStop();

        FractionManageHelper.release(getFractionManager());
    }

    private int getLayoutRes(String key) {
        switch (key) {
            case Args.ARG_TOUCH_EVENT:
                return ResourceTable.Layout_ability_touch_event;

            case Args.ARG_NESTED_SCROLLING:
                return ResourceTable.Layout_ability_nested;

            case Args.ARG_COORDINATOR:
                return ResourceTable.Layout_ability_coordinator;

            case Args.ARG_COORDINATOR_LIST:
                return ResourceTable.Layout_ability_coordinator_list;

            case Args.ARG_COORDINATOR_SLIDER_LIST:
                return ResourceTable.Layout_ability_coordinator_viewpager;
        }
        return 0;
    }

    private void onInit(String key) {
        if (key.equals(Args.ARG_COORDINATOR_LIST)) {
            initList();
        }

        if (key.equals(Args.ARG_COORDINATOR_SLIDER_LIST)) {
            initViewPager();
        }
    }

    private void initList() {
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_container);
        container.setItemProvider(new SampleProvider());

        container.setItemClickedListener((list, component, position, id) -> {
            new ToastDialog(this)
                    .setText("you clicked:" + position)
                    .setAlignment(LayoutAlignment.CENTER)
                    .show();
        });
    }

    private void initViewPager() {
        ViewPager pager = (ViewPager) findComponentById(ResourceTable.Id_pager);

        List<Component> texts = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            Text text = new Text(getContext());
            ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
//            text.setLayoutConfig(config);
            text.setText(j + "");
            text.setTextSize(100);
            text.setTextAlignment(TextAlignment.CENTER);

            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(100 + (155 / (j + 1)), 100 + (155 / (j + 1)), 100 + (155 / (j + 1))));
            text.setBackground(shapeElement);

            texts.add(text);
        }

        List<Fraction> fractions = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            fractions.add(new PagerFraction(i));
        }
        pager.setAdapter(new FractionAdapter(getFractionManager(), fractions));
//        pager.setAdapter(new ViewPagerAdapter(texts));
//        pager.setCurrentItem(2);
        pager.setOffscreenPageLimit(1);
        pager.setPageTransformer(false, new DepthPageTransformer());

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class SampleProvider extends BaseItemProvider {

        @Override
        public int getCount() {
            return 100;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convert, ComponentContainer container) {
            final Component cpt;
            if (convert == null) {
                cpt = LayoutScatter.getInstance(ArgsAbility.this).parse(ResourceTable.Layout_item, null, false);
            } else {
                cpt = convert;
            }

            Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_text);
            text.setText("text" + position);
            return cpt;
        }
    }

    static class FractionAdapter extends FractionPagerAdapter {

        private final List<Fraction> components;

        public FractionAdapter(FractionManager fm, List<Fraction> fractions) {
            super(fm);
            components = fractions;
        }

        @Override
        public Fraction getItem(int position) {
            return components.get(position);
        }

        @Override
        public int getCount() {
            return components.size();
        }
    }

    static class ViewPagerAdapter extends PagerAdapter {

        private final List<Component> components;

        public ViewPagerAdapter(List<Component> components) {
            this.components = components;
        }

        @Override
        public int getCount() {
            return components.size();
        }

        @Override
        public boolean isViewFromObject(Component view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ComponentContainer container, int position) {
            Component component = components.get(position);
            container.addComponent(component);
            return component;
        }

        @Override
        public void destroyItem(ComponentContainer container, int position, Object object) {
            container.removeComponent((Component) object);
        }
    }
}
